package com.ft.mybatis.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ft.core.domain.entities.IEntity;
import com.ft.core.function.Func_1;
import com.ft.core.utils.AutoMapUtils;
import com.ft.mybatis.service.IMyBatisBaseService;

import java.util.List;

/**
 * @copyright (C), 2015-2019, 梵途科技有限公司
 * @fileName: MyBatisBaseServiceImpl
 * @author: 李阳
 * @date: 2019/4/18 16:05
 * @description: 基于Mybatis-plus Service 层通用实现
 */
public class MyBatisBaseServiceImpl<TEntity extends IEntity>
        extends ServiceImpl<BaseMapper<TEntity>, TEntity>
        implements IMyBatisBaseService<TEntity> {

    @Override
    public <U> IPage<U> page(long pageIndex, long pageSize, Wrapper<TEntity> queryWrapper,Class<U> uClass) {
        Page<TEntity> page = new Page<>(pageIndex, pageSize);
        IPage pageResult = this.page(page, queryWrapper);
        pageResult.setRecords(AutoMapUtils.mapList(pageResult.getRecords(),uClass));
        return pageResult;
    }

    @Override
    public <U> IPage<U> page(long pageIndex, long pageSize, Wrapper<TEntity> queryWrapper, Func_1<List<TEntity>, List<U>> func) {
        Page<TEntity> page = new Page<>(pageIndex, pageSize);
        IPage pageResult = this.page(page, queryWrapper);
        List<U> apply = func.apply(pageResult.getRecords());
        pageResult.setRecords(apply);
        return pageResult;
    }
}
